/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.net.greenet.generalfile.servlet;

import cn.net.greenet.generalfile.logic.GeneralFileProcess;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 *
 * @author Administrator
 */
@WebServlet(name = "Add_IPBank", urlPatterns = {"/common/add_ip_bank"})
public class Add_IPBank extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String content = request.getParameter("content");
        JSONArray arr = JSONArray.fromObject(content);
        PrintWriter out = response.getWriter();
        boolean flag = false;
        GeneralFileProcess pro = new GeneralFileProcess();
        if (!arr.isEmpty()) {
            pro.init();
            Map<String, Integer> map = pro.getareids();
            for (int i = 0; i < arr.size(); i++) {
                JSONObject one = arr.getJSONObject(i);
                String ipType = one.getString("IPType");
                String areaName = one.getString("areaName");
                String startIP = one.getString("startIP");
                String endIP = one.getString("endIP");
                int areaNameId = map.size() + 1;
                if (map.containsKey(areaName)) {
                    areaNameId = map.get(areaName);
                } else {
                    map.put(areaName, areaNameId);
                }
                pro.insert(GeneralFileProcess.INSERT_IP_BANK, new Object[]{areaName, startIP, endIP, ipType, areaNameId});
            }
            flag = true;
        }
        pro.close();
        try {
            
        } finally {
            out.print(flag);
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
